package com.lagou.common;

import com.lagou.zookeeper.RPCRegistryHandler;
import com.lagou.zookeeper.config.ConfigKeeper;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

@Component
public class RpcRegistryFactory implements FactoryBean<RPCRegistryHandler>, DisposableBean {

    private RPCRegistryHandler registryHandler;

    @Override
    public void destroy() throws Exception {
        System.out.println("RpcRegistryFactory ### destroy ...");
        registryHandler.destroy();
    }

    @Override
    public RPCRegistryHandler getObject() throws Exception {
        if(this.registryHandler == null){
            registryHandler = new ZkRegistryHandler(ConfigKeeper.getInstance().getZkAddr());
        }
        return  registryHandler;
    }

    @Override
    public Class<?> getObjectType() {
        System.out.println("RpcRegistryFactory ### getObjectType ...");
        return RPCRegistryHandler.class;
    }
}
